#include "cswriter.h"
#include "properties\pefactory.h"
#include "qtvariantproperty.h"
#include "CCQTSceneDirector.h"
#include "csreader.h"
CSWriter::CSWriter()
{
}

CSWriter s_write;

CSWriter *CSWriter::sharedWriter()
{
    return &s_write;
}

void CSWriter::generateXML( QTreeWidgetItem * item , xmlTextWriterPtr writer, bool isProjFile )
{
    QCocoNode * node = (QCocoNode*)item->data(0,Qt::UserRole).value<int>();
    qDebug()<<node->name;
	std::string name(node->name.toLocal8Bit());
	//get data from the peditor
	CCQTSceneDirector::sharedSceneDirector()->setCurrentQNode(node);
	QTreeNode <QCocoNode> * treeNode = CocoHieriachy::sharedHieriachy()->getTreeNodeById(node->id);
	QVector<QCocoNode> superNodes =  treeNode->getSupertypes();
	xmlTextWriterStartElement(writer ,  (xmlChar*)(name.c_str()));
	xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"id" ,"%d" ,node->id);
	for (int i = superNodes.size() -1;i >=0 ; i --)
	{
		// get peditor by name .
		PEditor * peditor = PEFactory::shareFactory()->getPEditorByName(superNodes.at(i).name);
		if(peditor == NULL )continue;

		peditor->refresh();
		// get peditor all properties .
		QList<QString> listnames =  peditor->getAllPropertyNames();
		
		for (int i =0;i <listnames.size() ;i++)
		{

			QtVariantProperty *qproperty =  peditor->Property(listnames.at(i));
			std::string elementNodeName(  listnames.at(i).toLocal8Bit().data() );
			if (QtVariantPropertyManager::enumTypeId() == qproperty->propertyType())
			{

				QStringList enumList = qproperty->attributeValue("enumNames").value<QStringList>();
				std::string imagePath = enumList[qproperty->value().value<int>()].toLocal8Bit().data();
				xmlTextWriterStartElement(writer , (xmlChar*) elementNodeName.c_str());
				if (isProjFile)xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"type" ,"%d", TYPE_ENUMETYPE);
				xmlTextWriterWriteString(writer ,  (xmlChar*) imagePath.c_str());
				xmlTextWriterEndElement(writer);
				continue;
			}
			QString s;
			xmlTextWriterStartElement(writer , (xmlChar*) elementNodeName.c_str());
			if (isProjFile)xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"type" ,"%d" ,qproperty->propertyType() );
			switch(qproperty->propertyType() )
			{
			case QVariant::String :
					s = qproperty->value().value<QString>();

					xmlTextWriterWriteString(writer, (xmlChar*) s.toLocal8Bit().data());
					
					break;
			case QVariant::Int :
					s = QString::number(qproperty->value().value<int>());

					xmlTextWriterWriteString(writer, (xmlChar*) s.toLocal8Bit().data());

					//xmlTextWriterWriteElement(writer , (xmlChar*) elementNodeName.c_str() ,  (xmlChar*) s.toLocal8Bit().data());
					break;
			case QVariant::Double :
					s = QString::number(qproperty->value().value<double>());

					xmlTextWriterWriteString(writer, (xmlChar*) s.toLocal8Bit().data());

					//xmlTextWriterWriteElement(writer , (xmlChar*) elementNodeName.c_str() ,  (xmlChar*) s.toLocal8Bit().data());
					break;
			case QVariant::PointF:
				{
					QPointF myPoint = qproperty->value().value<QPointF>();
					//qDebug()<<myPoint.x() <<","<<myPoint.y();
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"x" ,"%.2f" ,myPoint.x());
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"y" ,"%.2f" ,myPoint.y());
					break;
				}
			case QVariant::SizeF:
				{
					QSizeF mysize  = qproperty->value().value<QSizeF>();

					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"width" ,"%.2f" ,mysize.width());
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"height" ,"%.2f" ,mysize.height());

					break;
				}
			case QVariant::Color:
				{
					QColor  color  = qproperty->value().value<QColor>();

                    xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"r" ,"%d" ,color.red());
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"g" ,"%d" ,color.green());
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"b" ,"%d" ,color.blue());
					xmlTextWriterWriteFormatAttribute(writer , (xmlChar*)"a" ,"%d" ,color.alpha());

					break;
				}
            case QVariant::Bool:
            {
                bool qb  = qproperty->value().value<bool>();
                if (qb) s = "true";
                else s = "false";
                xmlTextWriterWriteString(writer, (xmlChar*) s.toLocal8Bit().data());
            }
			default:
				{
					//xmlTextWriterWriteFormatAttribute
// 					xmlTextWriterStartElement(writer ,  (xmlChar*)(listnames.at(i).toLocal8Bit().data()));
// 					xmlTextWriterEndElement(writer);
					break;
				}
			}
			xmlTextWriterEndElement(writer);
		}
	}

    for (int i =0 ;i < item->childCount();i ++)
    {
        generateXML(item->child(i) ,writer, isProjFile);
    }
    xmlTextWriterEndElement(writer);
}

void CSWriter::writeCSC(QString  destFile , bool isprojF )
{
    xmlDocPtr doc;
    xmlTextWriterPtr writer = xmlNewTextWriterDoc(&doc, 0);
    int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
    xmlTextWriterStartElement(writer ,  (xmlChar*)"CCScene");
   

     MainWindow *mainwindow =ConstantManager::sharedManager()->qWindow();
     TreeWidget_CCScene * treeWidget = qFindChild<TreeWidget_CCScene*>(mainwindow , "treeWidget_nodes");

     for (int i = 0 ;i < treeWidget->topLevelItemCount();i ++)
     {   
         generateXML(treeWidget->topLevelItem(i) ,  writer , isprojF);
     }

    xmlTextWriterEndElement(writer);
	xmlTextWriterEndDocument(writer);
	xmlFreeTextWriter(writer);
    xmlSaveFileEnc(destFile.toLocal8Bit() , doc ,"utf-8");	
    xmlFreeDoc(doc);
}

void CSWriter::writeCSCProjFile(QString destFile)
{
    writeCSC(destFile, true);
}


